/*******************************************************************************
 * Copyright 2010 DemandSpot
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package com.demandspot.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.CloseEvent;
import com.google.gwt.event.logical.shared.CloseHandler;
import com.google.gwt.i18n.client.Messages;
import com.google.gwt.i18n.client.LocalizableResource.DefaultLocale;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DecoratedPopupPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.PopupPanelUtil;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.widgetideas.client.GlassPanel;

public class PopupFactory {

	private static final GlassPanelUtil gpUtil = GWT.create(GlassPanelUtil.class); //late bing glass panel to make sure it does not appear on IE
	
	@DefaultLocale("en")
	private interface Labels extends Messages {
		
		@DefaultMessage("OK")
		String ok();

		@DefaultMessage("Cancel")
		String cancel();		
		
		@DefaultMessage("Close")
		String close();
		
		public static class Util {
			private static final Labels LABELS = GWT.create(Labels.class);	
			
			public static final Labels getLabels() {		
				return LABELS;
			}		 
		}

				
	}
	
	private static DecoratedPopupPanel create(String title, Widget body, boolean isModal, boolean closable) {
		final GlassPanel glassPanel = gpUtil.show();
		
		DecoratedPopupPanel popup = new DecoratedPopupPanel();
		VerticalPanel wrapper = new VerticalPanel();
		wrapper.add(createHeader(popup, title, closable));
		wrapper.add(body);
				
		popup.setModal(isModal);
		
		popup.add(wrapper);
		
		popup.addCloseHandler(new CloseHandler<PopupPanel>() {
			public void onClose(CloseEvent<PopupPanel> evt) {
				glassPanel.removeFromParent();
			}			
		});
		
		PopupPanelUtil.useRollDownAnimation(popup);
		
		return popup;
	}
	
	private static DecoratedPopupPanel createOkCancel(String title, VerticalPanel body, boolean isModal, String okButtonLabel, final Command onOkCommand) {
		VerticalPanel okCancelPanel = new VerticalPanel();
		okCancelPanel.add(body);
				
		HorizontalPanel buttons = new HorizontalPanel();
		buttons.setWidth("100%");
		buttons.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		
		HorizontalPanel buttonsInner = new HorizontalPanel();
		buttonsInner.setSpacing(5);
		Button ok = new Button(okButtonLabel);
		Button cancel = new Button(Labels.Util.getLabels().cancel());
		
		if (okButtonLabel.equals(Labels.Util.getLabels().ok())) {
			ok.setWidth("60px");
		}
		
		cancel.setWidth("60px");
		buttonsInner.add(ok);
		buttonsInner.add(cancel);
		buttons.add(buttonsInner);
		
		okCancelPanel.add(buttons);
		
		final DecoratedPopupPanel popup = create(title, okCancelPanel, isModal, true);
				
		cancel.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent evt) {
				popup.hide();
			}			
		});
		
		ok.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent evt) {
				popup.hide();
				onOkCommand.execute();	
			}			
		});
		
		return popup;
	}
	
	public static DecoratedPopupPanel show(String title, Widget body, boolean isModal, boolean closable) {
		DecoratedPopupPanel popup = create(title, body, isModal, closable);
		popup.addStyleName(Styles.Util.getStyles().ds().zIndex100());
		popup.center();
		popup.show();
		return popup;
	}	
	
	public static DecoratedPopupPanel showOkCancel(String title, VerticalPanel body, boolean isModal, final Command onOkCommand) {
		return showOkCancel(title, body, isModal, Labels.Util.getLabels().ok(), onOkCommand);
	}

	public static DecoratedPopupPanel showOkCancel(String title, VerticalPanel body, boolean isModal, String okButtonLabel, final Command onOkCommand) {
		DecoratedPopupPanel popup = createOkCancel(title, body, isModal, okButtonLabel, onOkCommand);
		popup.center();
		popup.show();
		return popup;
	}
	
	private static Widget createHeader(final DecoratedPopupPanel popup, String title, boolean closable) {
		FlexTable header = new FlexTable();
		header.setWidth("100%");
		header.setStyleName(Styles.Util.getStyles().ds().popupTitle());
		
		header.setHTML(0, 0, title);
		if (closable) {
			HTML closeBtn = new HTML("x");				
			closeBtn.setTitle(Labels.Util.getLabels().close());
			closeBtn.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {
					popup.hide();
				}			
			});
			closeBtn.setStyleName(Styles.Util.getStyles().ds().popupTitleCloseBtn());		
			header.setWidget(0, 1, closeBtn);
			header.getFlexCellFormatter().setAlignment(0, 1, HasHorizontalAlignment.ALIGN_RIGHT, HasVerticalAlignment.ALIGN_MIDDLE);
		}
		
		header.getRowFormatter().setVerticalAlign(0, HasVerticalAlignment.ALIGN_MIDDLE);
		header.getFlexCellFormatter().setWidth(0, 0, "100%");
		
		return header;
	}
	
}
